/**
 * The MIT License	
 * 
 * Copyright (c) 2009 Ghost Interactive
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * ghostinteractive@gmail.com
 * http://ghostinteractive.net/blog
 * @author Jeremy brown
 */
package net.ghostinteractive.overlays
{
	import flash.geom.Point;
	
	public class RectangleGate extends ProximityGate
	{
		/**
		 * Width of the rectangular region
		 */ 
		protected var _width	:Number;
		
		/**
		 * Height of the rectangular region
		 */ 
		protected var _height	:Number;
		
		/**
		 * This sub-class of the ProximityGate defines a rectanglar region of proximity
		 * @param _initPosition The center of the rectangular region
		 * @param _initWidth    The width of the rectangular region
		 * @param _initHeight	The height of the rectangular region
		 * @param _initZoomMin Zoom levels below this value will cause gate to close
		 * @param _initZoomMax Zoom levels above this value will cause gate to close
		 */
		public function RectangleGate( _initPosition:Point,
									   _initWidth:Number, _initHeight:Number, 
									   _initZoomMin:Number, _initZoomMax:Number )
		{
			super();
			_position	= _initPosition;
			_width 		= _initWidth;
			_height		= _initHeight;
			_zmin 		= _initZoomMin;
			_zmax		= _initZoomMax;
		}
		
		/**
		 * Evaluate the passed distance and zoom to determine whether or not 
		 * to open or close the gate
		 */
		override public function update( center:Point, zoom:Number ):void
		{
			var dx:Number = Math.abs( center.x - _position.x );
			var dy:Number = Math.abs( center.y - _position.y );

			if( dx <= _width / 2 &&  dy <= _height / 2 && zoomIsInRange( zoom ) ) {
				setState( OPENED );
			} else {
				setState( CLOSED );
			}
		}
	}
}